package com.xunqi.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xunqi.mall.enums.ProductStatusEnum;
import com.xunqi.mall.enums.ResponseEnum;
import com.xunqi.mall.mapper.ProductMapper;
import com.xunqi.mall.pojo.Product;
import com.xunqi.mall.service.CategoryService;
import com.xunqi.mall.service.ProductService;
import com.xunqi.mall.vo.ProductDetailVo;
import com.xunqi.mall.vo.ProductVo;
import com.xunqi.mall.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Created with IntelliJ IDEA.
 * @author: 夏沫止水
 * @create: 2020-04-23 21:14
 **/
@Service("productService")
@Slf4j
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Resource
    private CategoryService categoryService;

    /**
     * 可根据类目id和分页查询商品信息
     * @param categoryId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResponseVo<PageInfo> list(Integer categoryId, Integer pageNum, Integer pageSize) {

        //将商品的类目信息和子类目给查询出来，获得类目id集合
        Set<Integer> categoryIdSet = new HashSet<Integer>();

        if (categoryId != null) {
            categoryService.findSubCategoryId(categoryId,categoryIdSet);
            categoryIdSet.add(categoryId);

        }

        //设置分页条件
        PageHelper.startPage(pageNum,pageSize);

        //再去通过id集合查询商品列表信息
        List<Product> productList = productMapper.selectByCategoryIdSet(categoryIdSet);
        log.info("productList={}",productList);

        //转换程productVoList集合
        List<ProductVo> productVoList = productList.stream().map(e -> {
            ProductVo productVo = new ProductVo();
            BeanUtils.copyProperties(e, productVo);
            return productVo;
        }).collect(Collectors.toList());

        PageInfo pageInfo = new PageInfo(productList);
        pageInfo.setList(productVoList);

        return ResponseVo.success(pageInfo);
    }

    /**
     * 根据类目id查询商品信息
     * @param categoryId
     * @return
     */
    @Override
    public ResponseVo<ProductDetailVo> detail(Integer categoryId) {

        //查询商品详情
        Product product = productMapper.selectByCategoryId(categoryId);

        //商品不存在
        if (product == null) {
            return ResponseVo.error(ResponseEnum.PRODUCT_OFF_SALE_OR_DELETE);
        }

        //商品状态
        Integer status = product.getStatus();

        //判断商品状态是否是在架状态
        if (status.equals(ProductStatusEnum.DELETE.getCode())
                || status.equals(ProductStatusEnum.OFF_SALE.getCode())) {
            return ResponseVo.error(ResponseEnum.PRODUCT_OFF_SALE_OR_DELETE);
        }

        ProductDetailVo productDetailVo = new ProductDetailVo();
        BeanUtils.copyProperties(product,productDetailVo);

        //对敏感数据的处理，如果库存数量超过100，就隐藏真实库存数量
        productDetailVo.setStock(productDetailVo.getStock() > 100 ? 100 : productDetailVo.getStock());

        return ResponseVo.success(productDetailVo);
    }
}
